﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VBACodeingTool
{
    public class CodeData
    {
        private string content;
        private bool isLastUse;
        private int score;
        private string shortCut;

        public CodeData(bool isLastUse, string shortCut, string content, int score)
        {
            IsLastUse = isLastUse;
            ShortCut = shortCut;
            Content = content;
            Score = score;
        }

        public string Content { get => content; set => content = value; }

        public bool IsLastUse { get => isLastUse; set => isLastUse = value; }

        public int Score { get => score; set => score = value; }

        public string ShortCut { get => shortCut; set => shortCut = value; }

        public int GetScore(string input)
        {
            Score = 40;
            int wrongCounter = 0;
            int inputCounter = 0;
            int continuoSucceedCounter = 0;
            int continuoWrongCounter = 0;
            bool firstContinuo = false;
            int firstContinuoCounter = 0;
            List<int> continuoSucceeds = new List<int>();
            List<int> continuoWrongs = new List<int>();
            input = input.ToLower();
            string newShortCut = ShortCut.ToLower();
            for (int i = 0; i < newShortCut.Length; i++)
            {
                if (i>input.Length-1)
                {
                    if (firstContinuoCounter==0)
                    {
                        firstContinuoCounter = continuoSucceedCounter;
                    }
                    CalculateSumScore(firstContinuoCounter, continuoSucceeds, continuoWrongs, newShortCut);
                    return Score;
                }
                //这里两边不同时使用i作为下标,是要做模糊匹配
                if (newShortCut[i] != input[inputCounter])
                {
                    wrongCounter++;
                    continuoWrongCounter++;
                    Score--;
                    if (continuoSucceedCounter != 0)
                    {
                        if (!firstContinuo)
                        {
                            firstContinuoCounter = continuoSucceedCounter;
                        }
                        else
                        {
                            continuoSucceeds.Add(continuoSucceedCounter);
                        }
                    }
                    continuoSucceedCounter = 0;
                    if (i == 0)
                    {
                        Score = 0;
                        return Score;
                    }
                }
                else
                {
                    inputCounter++;
                    continuoSucceedCounter++;
                    if (continuoWrongCounter != 0)
                    {
                        continuoWrongs.Add(continuoWrongCounter);
                    }
                    continuoWrongCounter = 0;
                }

            }
            if (firstContinuoCounter == 0)
            {
                firstContinuoCounter = continuoSucceedCounter;
            }
            CalculateSumScore(firstContinuoCounter, continuoSucceeds, continuoWrongs, newShortCut);
            return Score;
        }

        private void CalculateSumScore(int firstContinuoCounter, List<int> continuoSucceeds, List<int> continuoWrongs, string newShortCut)
        {
            if (newShortCut.Length < 4)
            {
                CalculateScore(6, 3, continuoSucceeds, continuoWrongs);
                Score += firstContinuoCounter * 10;
            }
            else if (newShortCut.Length < 7)
            {
                CalculateScore(4, 5, continuoSucceeds, continuoWrongs);
                Score += firstContinuoCounter * 10;
            }
            else if (newShortCut.Length < 13)
            {
                CalculateScore(3, 7, continuoSucceeds, continuoWrongs);
                Score += firstContinuoCounter * 10;
            }
            else if (newShortCut.Length < 20)
            {
                CalculateScore(2, 10, continuoSucceeds, continuoWrongs);
                Score += firstContinuoCounter * 10;
            }
            else
            {
                CalculateScore(1, 14, continuoSucceeds, continuoWrongs);
                Score += firstContinuoCounter * 10;
            }
        }

        private void CalculateScore(int addStandard, int minusStandard, List<int> succeeds, List<int> wrongs)
        {
            foreach (int succeedCounter in succeeds)
            {
                Score += addStandard * succeedCounter;
            }
            foreach (int wrongCounter in wrongs)
            {
                Score -= minusStandard * wrongCounter;
            }
        }
    }
}